﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.Collections;
using System.Xml;
using System.Linq;
//using System.Xml.Linq;

using CommonUtils;
using TDAL = EzDAL.Auto2;
using TDALUtils = EzDAL.Utils;
using TConst = TERP.Constants;
using TUtils = TERP.Utils;
using TError = TERP.Errors;


namespace TERP.BizITS
{
    public partial class BizWater
	{
		#region // Private Utils:
		#endregion

        #region //Validate

        #region //StockOut
		#region //CheckExistStockOutID
		private void CheckExistStockOutID(
           ref ArrayList alParamsCoupleError
           , string strStockOutID
           , out DataTable dtStockOut
        )
        {
			dtStockOut = TDALUtils.DBUtils.GetTableContents(
                _dbCarSv // db
                , "Water_StockOut" // strTableName
                , "top 1 *" // strColumnList
                , "" // strClauseOrderBy
				, "StockOutID", "=", strStockOutID // arrobjParamsTriple item
             );

			if (dtStockOut == null || dtStockOut.Rows.Count == 0)
            {

                throw CMyException.Raise(
                    TError.ErrITS.Its_StockOutID_Not_Found
                    , null
                    , alParamsCoupleError.ToArray()
                    );
            }
        }
        #endregion
        #endregion


        #endregion

        #region //StockOut

        #region Its_StockOut_Get
		public DataSet Its_StockOut_Get(
        string strGWUserCode
        , string strGWPassword
        , string strTid
        , string strPartnerCode
        , string strPartnerUserCode
        , string strLanguageCode
        , string strStockIDConditionList
        , string strStockOutDateConditionList
        , string strUnitConditionList
		, string strCusIDConditionList
		, string strCusNameConditionList
        , string strStatusConditionList
        )
        {

            #region // Temp:

            DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);

            int nTidSeq = 0;
            bool bNeedTransaction = false;
			string strFunctionName = "Its_StockOut_Get";
			string strErrorCodeDefault = TError.ErrITS.Its_StockOut_Get;
            ArrayList alParamsCoupleError = new ArrayList(new object[]{

					"strFunctionName", strFunctionName
                    , "strStockIDConditionList", strStockIDConditionList
					, "strStockOutDateConditionList", strStockOutDateConditionList
					, "strUnitConditionList", strUnitConditionList
                    ,"strCusIDConditionList",strCusIDConditionList
					,"strCusNameConditionList",strCusNameConditionList
                    , "strStatusConditionList",strStatusConditionList
					});

            #endregion

            try
            {

                #region // Init:

                _dbCarSv.AppUserID = strPartnerUserCode;

                if (bNeedTransaction) _dbCarSv.BeginTransaction();

                // Write request log:

                _log.WriteLogAsync(

                _strConfig_GwUserCode // strGwUserCodeForLog

                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog

                , strTid // strAppTid

                , strPartnerCode // strAppServiceCode

                , strPartnerUserCode // strAppUPartCode

                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

                , TError.Error.NoError // strAppErrorCode

                , strFunctionName // strAppLogType1

                , "REQUEST" // strAppLogType2

                , "" // strAppLogType3

                , strLanguageCode // strAppLanguageCode

                , "" // strAppRemark

                , TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple

                );

                // Check Valid Tid:

                myUtils_ValidateId(

                ref alParamsCoupleError // alParamsCoupleError

                , strTid // strTid

                , TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid

                );

                #endregion

                #region // Build Sql:

                ArrayList alParamsCoupleSql = new ArrayList();

                string strSqlGetData = @"
						SELECT
                                t.*
								, cus.CusName
								, t.Price*t.Quantity AS AMOUNT
								, t.Price*t.Quantity + t.PriceCuocVo*t.CuocVo -t.AmountPaid AS AMOUNTDEBIT
                        FROM Water_StockOut t with (nolock)
						left join Water_Customer cus on t.CusID = cus.CusID
						WHERE (1=1)
                                    zzzzClauseWhereStockOutIDConditionList
                                    zzzzClauseWhereStockOutDateConditionList
									zzzzClauseWhereCusIDConditionList
									zzzzClauseWhereCusNameConditionList
									zzzzClauseWhereStatusConditionList
									;
				";

				string zzzzClauseWhereStockOutIDConditionList = SqlUtils.BuildClause("and", "t.StockOutID", strStockIDConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereStockOutDateConditionList = SqlUtils.BuildClause("and", "t.StockOutDate", strStockOutDateConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereCusIDConditionList = SqlUtils.BuildClause("and", "t.CusID", strCusIDConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereCusNameConditionList = SqlUtils.BuildClause("and", "cus.CusName", strCusNameConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereStatusConditionList = SqlUtils.BuildClause("and", "t.Status", strStatusConditionList, "@p", ref alParamsCoupleSql);
				

                strSqlGetData = StringUtils.Replace(
                strSqlGetData
				, "zzzzClauseWhereStockOutIDConditionList", zzzzClauseWhereStockOutIDConditionList
				, "zzzzClauseWhereStockOutDateConditionList", zzzzClauseWhereStockOutDateConditionList
				, "zzzzClauseWhereCusIDConditionList", zzzzClauseWhereCusIDConditionList
				, "zzzzClauseWhereCusNameConditionList", zzzzClauseWhereCusNameConditionList
				, "zzzzClauseWhereStatusConditionList", zzzzClauseWhereStatusConditionList

                );

                #endregion

                #region // Get Data:
                myDebug_SaveSql(
                    _bDebugMode // bDebugMode
                    , ref alParamsCoupleError // alParamsCoupleError
                    , string.Format("strSql_{0}", strFunctionName) // strParamName
                    , strSqlGetData // strSql
                    );


                DataSet dsGetData = _dbCarSv.ExecQuery(

                strSqlGetData

                , alParamsCoupleSql.ToArray()

                );

                dsGetData.Tables[0].TableName = "Water_StockOut";
            
                DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

                #endregion

                // Return Good:

                TDALUtils.DBUtils.CommitSafety(_dbCarSv);

                mdsFinal.AcceptChanges();

                return mdsFinal;

            }

            catch (Exception exc)
            {

                #region // Catch of try:

                // Rollback:

                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

                // Return Bad:

                return TUtils.CProcessException.Process(

                ref mdsFinal

                , exc

                , strErrorCodeDefault
                , alParamsCoupleError.ToArray()

                );

                #endregion

            }

            finally
            {

                #region // Finally of try:

                // Rollback and Release resources:

                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

                TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);

                TUtils.CProcessException.Process(

                ref mdsFinal // mdsFinal

                , _log // log

                , _strConfig_GwUserCode // strGwUserCodeForLog

                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog

                , strFunctionName // strFunctionName

                , TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid

                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

                , strPartnerCode // strAppServiceCode

                , strPartnerUserCode // strAppUserCode

                , strLanguageCode // strLanguageCode

                , "" // strRemark"

                );

                #endregion

            }

        }

        #endregion

		#region Its_StockOut_Get_Rpt
		public DataSet Its_StockOut_Get_Rpt(
		string strGWUserCode
		, string strGWPassword
		, string strTid
		, string strPartnerCode
		, string strPartnerUserCode
		, string strLanguageCode
		, string strStockIDConditionList
		, string strStockOutDateConditionList
		, string strUnitConditionList
		, string strCusIDConditionList
		, string strCusNameConditionList
		)
		{

			#region // Temp:

			DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);

			int nTidSeq = 0;
			bool bNeedTransaction = false;
			string strFunctionName = "Its_StockOut_Get_Rpt";
			string strErrorCodeDefault = TError.ErrITS.Its_StockOut_Get_Rpt;
			ArrayList alParamsCoupleError = new ArrayList(new object[]{

					"strFunctionName", strFunctionName
                    , "strStockIDConditionList", strStockIDConditionList
					, "strStockOutDateConditionList", strStockOutDateConditionList
					, "strUnitConditionList", strUnitConditionList
                    , "strCusIDConditionList",strCusIDConditionList
					, "strCusNameConditionList",strCusNameConditionList
					});

			#endregion

			try
			{

				#region // Init:

				_dbCarSv.AppUserID = strPartnerUserCode;

				if (bNeedTransaction) _dbCarSv.BeginTransaction();

				// Write request log:

				_log.WriteLogAsync(

				_strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strTid // strAppTid

				, strPartnerCode // strAppServiceCode

				, strPartnerUserCode // strAppUPartCode

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, TError.Error.NoError // strAppErrorCode

				, strFunctionName // strAppLogType1

				, "REQUEST" // strAppLogType2

				, "" // strAppLogType3

				, strLanguageCode // strAppLanguageCode

				, "" // strAppRemark

				, TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple

				);

				// Check Valid Tid:

				myUtils_ValidateId(

				ref alParamsCoupleError // alParamsCoupleError

				, strTid // strTid

				, TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid

				);

				#endregion

				#region // Build Sql:

				ArrayList alParamsCoupleSql = new ArrayList();

				string strSqlGetData = @"
						SELECT
                                t.*
								, cus.CusCode
								, cus.CusName
								, t.Price*t.Quantity AS AMOUNT
								, t.Price*t.Quantity + t.PriceCuocVo*t.CuocVo -t.AmountPaid AS AMOUNTDEBIT
						into #tbl_StockOut_Rpt
                        FROM Water_StockOut t with (nolock)
						left join Water_Customer cus on t.CusID = cus.CusID
						WHERE (1=1)
                                    zzzzClauseWhereStockOutIDConditionList
                                    zzzzClauseWhereStockOutDateConditionList
									zzzzClauseWhereCusIDConditionList
									zzzzClauseWhereCusNameConditionList
						
						select 
								t.StockOutDate
								, t.CusCode
								, t.CusName
								, Sum(t.Quantity) AS SumQuantity
								, Sum(t.Quantity * t.Price) AS SumAmount
								, Sum(t.NoVo) AS SumNoVo
								, Sum(t.CuocVo) AS SumCuocVo
								, t.Unit
						from #tbl_StockOut_Rpt t						
						group by t.StockOutDate,t.CusCode,t.CusName,t.Unit
									;
				";

				string zzzzClauseWhereStockOutIDConditionList = SqlUtils.BuildClause("and", "t.StockOutID", strStockIDConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereStockOutDateConditionList = SqlUtils.BuildClause("and", "t.StockOutDate", strStockOutDateConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereCusIDConditionList = SqlUtils.BuildClause("and", "t.CusID", strCusIDConditionList, "@p", ref alParamsCoupleSql);
				string zzzzClauseWhereCusNameConditionList = SqlUtils.BuildClause("and", "cus.CusName", strCusNameConditionList, "@p", ref alParamsCoupleSql);
				



				strSqlGetData = StringUtils.Replace(
				strSqlGetData
				, "zzzzClauseWhereStockOutIDConditionList", zzzzClauseWhereStockOutIDConditionList
				, "zzzzClauseWhereStockOutDateConditionList", zzzzClauseWhereStockOutDateConditionList
				, "zzzzClauseWhereCusIDConditionList", zzzzClauseWhereCusIDConditionList
				, "zzzzClauseWhereCusNameConditionList", zzzzClauseWhereCusNameConditionList

				);

				#endregion

				#region // Get Data:
				myDebug_SaveSql(
					_bDebugMode // bDebugMode
					, ref alParamsCoupleError // alParamsCoupleError
					, string.Format("strSql_{0}", strFunctionName) // strParamName
					, strSqlGetData // strSql
					);


				DataSet dsGetData = _dbCarSv.ExecQuery(

				strSqlGetData

				, alParamsCoupleSql.ToArray()

				);

				dsGetData.Tables[0].TableName = "Water_StockOut";

				DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

				#endregion

				// Return Good:

				TDALUtils.DBUtils.CommitSafety(_dbCarSv);

				mdsFinal.AcceptChanges();

				return mdsFinal;

			}

			catch (Exception exc)
			{

				#region // Catch of try:

				// Rollback:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				// Return Bad:

				return TUtils.CProcessException.Process(

				ref mdsFinal

				, exc

				, strErrorCodeDefault
				, alParamsCoupleError.ToArray()

				);

				#endregion

			}

			finally
			{

				#region // Finally of try:

				// Rollback and Release resources:

				TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

				TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);

				TUtils.CProcessException.Process(

				ref mdsFinal // mdsFinal

				, _log // log

				, _strConfig_GwUserCode // strGwUserCodeForLog

				, _strConfig_GwUserPasswordForLog // strGwPasswordForLog

				, strFunctionName // strFunctionName

				, TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid

				, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime

				, strPartnerCode // strAppServiceCode

				, strPartnerUserCode // strAppUserCode

				, strLanguageCode // strLanguageCode

				, "" // strRemark"

				);

				#endregion

			}

		}

		#endregion

        #region //Its_Water_StockOut_Create
		public DataSet Its_StockOut_Create(
			 string strGWUserCode
			 , string strGWPassword
			 , string strTid
			 , string strPartnerCode
			 , string strPartnerUserCode
			 , string strLanguageCode
			 , string strStockOutCode
			 , string strStockOutDate
			 , string strNote
			 , string strQuantity
			 , string strPrice
			 , string strPriceCuocVo
			 , string strUnit
			 , string strNoVo
			 , string strCuocVo
			 , string strCusID
			 , string strStatus
			 , string strAmountPaid
     )
        {
            #region // Temp:
            DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);
            int nTidSeq = 0;
            bool bNeedTransaction = true;
			string strFunctionName = "Its_Customer_Create";
            string strErrorCodeDefault = TError.ErrITS.Its_Customer_Create;
            ArrayList alParamsCoupleError = new ArrayList(new object[]{
			"strFunctionName", strFunctionName
			, "strStockOutCode", strStockOutCode
			, "strStockOutDate", strStockOutDate
			, "strNote", strNote
			, "strQuantity", strQuantity
			, "strPrice",strPrice
			, "strPriceCuocVo",strPriceCuocVo
			, "strUnit", strUnit
			, "strNoVo", strNoVo
			, "strCuocVo", strCuocVo
			, "strCusID", strCusID
			, "strStatus", strStatus
			, "strAmountPaid", strAmountPaid
});
            #endregion
            try
            {
                #region // Init:
                _dbCarSv.AppUserID = strPartnerUserCode;
                if (bNeedTransaction) _dbCarSv.BeginTransaction();
                // Write request log:
                _log.WriteLogAsync(
                _strConfig_GwUserCode // strGwUserCodeForLog
                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                , strTid // strAppTid
                , strPartnerCode // strAppServiceCode
                , strPartnerUserCode // strAppUPartCode
                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                , TError.Error.NoError // strAppErrorCode
                , strFunctionName // strAppLogType1
                , "REQUEST" // strAppLogType2
                , "" // strAppLogType3
                , strLanguageCode // strAppLanguageCode
                , "" // strAppRemark
                , TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple
                );
                // Check Valid Tid:
                myUtils_ValidateId(
                ref alParamsCoupleError // alParamsCoupleError
                , strTid // strTid
                , TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid
                );
                #endregion

                #region // Check:

				//if (dsBookGuideDetail == null || dsBookGuideDetail.Tables.Count == 0)
				//{
				//    throw CMyException.Raise(
				//   TError.ErrITS.Its_BookingTour_NotFoundDetail
				//   , null
				//   , alParamsCoupleError.ToArray()
				//   );
				//}
				//else
				//{
				//    if (dsBookGuideDetail.Tables["BookGuideDetail"].Rows.Count == 0)
				//    {
				//        throw CMyException.Raise(
				//    TError.ErrITS.Its_BookGuide_NotFoundDetail
				//    , null
				//    , alParamsCoupleError.ToArray()
				//  );
				//    }
                //}
                #endregion

                #region // dt_Book:
                DataTable dt_Book = TDALUtils.DBUtils.GetSchema(_dbCarSv, "Water_StockOut").Tables[0];
                dt_Book.Rows.Add(dt_Book.NewRow());
                dt_Book.Rows[0]["StockOutCode"] = strStockOutCode;
				if (!StringUtils.IsEmpty(strStockOutDate))
                {
					dt_Book.Rows[0]["StockOutDate"] = strStockOutDate;
                }

				if (!StringUtils.IsEmpty(strNote))
                {
					dt_Book.Rows[0]["Note"] = strNote;
                }

                if (!StringUtils.IsEmpty(strQuantity))
                {
					dt_Book.Rows[0]["Quantity"] = strQuantity;
                }

                if (!StringUtils.IsEmpty(strPrice))
                {
					dt_Book.Rows[0]["Price"] = strPrice;
                }
				if (!StringUtils.IsEmpty(strPriceCuocVo))
				{
					dt_Book.Rows[0]["PriceCuocVo"] = strPriceCuocVo;
				}
				if (!StringUtils.IsEmpty(strUnit))
				{
					dt_Book.Rows[0]["Unit"] = strUnit;
				}
				if (!StringUtils.IsEmpty(strNoVo))
				{
					dt_Book.Rows[0]["NoVo"] = strNoVo;
				}

				if (!StringUtils.IsEmpty(strCuocVo))
				{
					dt_Book.Rows[0]["CuocVo"] = strCuocVo;
				}
				if (!StringUtils.IsEmpty(strCusID))
				{
					dt_Book.Rows[0]["CusID"] = strCusID;
				}
				
                if (!StringUtils.IsEmpty(strStatus))
                {
					dt_Book.Rows[0]["Status"] = strStatus;
                }
				if (!StringUtils.IsEmpty(strAmountPaid))
				{
					dt_Book.Rows[0]["AmountPaid"] = strAmountPaid;
				}

                //Save
				_dbCarSv.SaveData("Water_StockOut", dt_Book);

                //Get ID
                string strSqlGetTourID = @"select @@Identity StockOutID;";
                Int32 iID = Convert.ToInt32(_dbCarSv.ExecQuery(strSqlGetTourID).Tables[0].Rows[0][0]);
				dt_Book.Rows[0]["StockOutID"] = iID;
                #endregion

				#region //Process
				double dGetAmount =Convert.ToDouble(strAmountPaid) - Convert.ToDouble(strPrice) * Convert.ToDouble(strQuantity);
				ProcessUpdateCustomerDebitMany(strCusID, dGetAmount.ToString(), strNoVo, strCuocVo);
				#endregion

				#region // Return data

				DataSet dsGetData = new DataSet();
                dsGetData.Tables.Add(dt_Book.Copy());
				dsGetData.Tables[0].TableName = "Water_StockOut";

                DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

                #endregion

                // Return Good:
                TDALUtils.DBUtils.CommitSafety(_dbCarSv);
                mdsFinal.AcceptChanges();
                return mdsFinal;
            }
            catch (Exception exc)
            {
                #region // Catch of try:
                // Rollback:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
                // Return Bad:
                return TUtils.CProcessException.Process(
                ref mdsFinal
                , exc
                , strErrorCodeDefault
                , alParamsCoupleError.ToArray()
                );
                #endregion
            }
            finally
            {
                #region // Finally of try:
                // Rollback and Release resources:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
                TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);
                TUtils.CProcessException.Process(
                ref mdsFinal // mdsFinal
                , _log // log
                , _strConfig_GwUserCode // strGwUserCodeForLog
                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                , strFunctionName // strFunctionName
                , TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid
                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                , strPartnerCode // strAppServiceCode
                , strPartnerUserCode // strAppUserCode
                , strLanguageCode // strLanguageCode
                , " // strRemark"
                );
                #endregion
            }
        }

        #endregion

		#region //Its_StockOut_Update
		public DataSet Its_StockOut_Update(
			 string strGWUserCode
			 , string strGWPassword
			 , string strTid
			 , string strPartnerCode
			 , string strPartnerUserCode
			 , string strLanguageCode
			, string strStockOutID
			, string strStockOutCode
			 , string strStockOutDate
			 , string strNote
			 , string strQuantity
			 , string strPrice
			 , string strPriceCuocVo
			 , string strUnit
			 , string strNoVo
			 , string strCuocVo
			 , string strCusID
			 , string strStatus
			 , string strAmountPaid
     )
        {
            #region // Temp:
            DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);
            int nTidSeq = 0;
            bool bNeedTransaction = true;
			string strFunctionName = "Its_Water_Customer_Update";
			string strErrorCodeDefault = TError.ErrITS.Its_Customer_Update;
            ArrayList alParamsCoupleError = new ArrayList(new object[]{
			"strFunctionName", strFunctionName
			,"strStockOutID",strStockOutID
			, "strStockOutCode", strStockOutCode
			, "strStockOutDate", strStockOutDate
			, "strNote", strNote
			, "strQuantity", strQuantity
			, "strPrice",strPrice
			, "strPriceCuocVo",strPriceCuocVo
			, "strUnit", strUnit
			, "strNoVo", strNoVo
			, "strCuocVo", strCuocVo
			, "strCusID", strCusID
			, "strStatus", strStatus
			, "strAmountPaid", strAmountPaid
});
            #endregion
            try
            {
                #region // Init:
                _dbCarSv.AppUserID = strPartnerUserCode;
                if (bNeedTransaction) _dbCarSv.BeginTransaction();
                // Write request log:
                _log.WriteLogAsync(
                _strConfig_GwUserCode // strGwUserCodeForLog
                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                , strTid // strAppTid
                , strPartnerCode // strAppServiceCode
                , strPartnerUserCode // strAppUPartCode
                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                , TError.Error.NoError // strAppErrorCode
                , strFunctionName // strAppLogType1
                , "REQUEST" // strAppLogType2
                , "" // strAppLogType3
                , strLanguageCode // strAppLanguageCode
                , "" // strAppRemark
                , TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple
                );
                // Check Valid Tid:
                myUtils_ValidateId(
                ref alParamsCoupleError // alParamsCoupleError
                , strTid // strTid
                , TError.ErrITS.CommonInvalidTid // strErrCodeWhenInvalid
                );
                #endregion

                #region // Check:
                DataTable dt_Book = null;
                this.CheckExistStockOutID(
                    ref alParamsCoupleError
                    , strCusID
                    , out dt_Book
                    );
                #endregion

                #region // dt_Book:
                ArrayList alColumnEffective = new ArrayList();
                alColumnEffective.Clear();

				dt_Book.Rows[0]["StockOutCode"] = strStockOutCode; ; alColumnEffective.Add("StockOutCode");
				if (!StringUtils.IsEmpty(strStockOutDate))
				{
					dt_Book.Rows[0]["StockOutDate"] = strStockOutDate; ; alColumnEffective.Add("StockOutDate");
				}

				if (!StringUtils.IsEmpty(strNote))
				{
					dt_Book.Rows[0]["Note"] = strNote; ; alColumnEffective.Add("Note");
				}

				if (!StringUtils.IsEmpty(strQuantity))
				{
					dt_Book.Rows[0]["Quantity"] = strQuantity; ; alColumnEffective.Add("Quantity");
				}

				if (!StringUtils.IsEmpty(strPrice))
				{
					dt_Book.Rows[0]["Price"] = strPrice; ; alColumnEffective.Add("Price");
				}
				if (!StringUtils.IsEmpty(strPriceCuocVo))
				{
					dt_Book.Rows[0]["PriceCuocVo"] = strPriceCuocVo; ; alColumnEffective.Add("PriceCuocVo");
				}
				if (!StringUtils.IsEmpty(strUnit))
				{
					dt_Book.Rows[0]["Unit"] = strUnit; ; alColumnEffective.Add("Unit");
				}
				if (!StringUtils.IsEmpty(strNoVo))
				{
					dt_Book.Rows[0]["NoVo"] = strNoVo; ; alColumnEffective.Add("NoVo");
				}

				if (!StringUtils.IsEmpty(strCuocVo))
				{
					dt_Book.Rows[0]["CuocVo"] = strCuocVo; ; alColumnEffective.Add("CuocVo");
				}
				if (!StringUtils.IsEmpty(strCusID))
				{
					dt_Book.Rows[0]["CusID"] = strCusID; ; alColumnEffective.Add("CusID");
				}

				if (!StringUtils.IsEmpty(strStatus))
				{
					dt_Book.Rows[0]["Status"] = strStatus; ; alColumnEffective.Add("Status");
				}
				if (!StringUtils.IsEmpty(strAmountPaid))
				{
					dt_Book.Rows[0]["AmountPaid"] = strAmountPaid; ; alColumnEffective.Add("AmountPaid");
				}

                //Save
                _dbCarSv.SaveData("Water_StockOut", dt_Book, alColumnEffective.ToArray());

                //Get ID
                Int32 iID = Convert.ToInt32(strCusID);
                #endregion

                #region // Return data
          

                //Return data
				DataSet dsGetData = new DataSet();
                dsGetData.Tables.Add(dt_Book.Copy());
				dsGetData.Tables[0].TableName = "Water_StockOut";

                DataUtils.MoveDataTable(ref mdsFinal, ref dsGetData);

                #endregion

                // Return Good:
                TDALUtils.DBUtils.CommitSafety(_dbCarSv);
                mdsFinal.AcceptChanges();
                return mdsFinal;
            }
            catch (Exception exc)
            {
                #region // Catch of try:
                // Rollback:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
                // Return Bad:
                return TUtils.CProcessException.Process(
                ref mdsFinal
                , exc
                , strErrorCodeDefault
                , alParamsCoupleError.ToArray()
                );
                #endregion
            }
            finally
            {
                #region // Finally of try:
                // Rollback and Release resources:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
                TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);
                TUtils.CProcessException.Process(
                ref mdsFinal // mdsFinal
                , _log // log
                , _strConfig_GwUserCode // strGwUserCodeForLog
                , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                , strFunctionName // strFunctionName
                , TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid
                , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                , strPartnerCode // strAppServiceCode
                , strPartnerUserCode // strAppUserCode
                , strLanguageCode // strLanguageCode
                , " // strRemark"
                );
                #endregion
            }
        }

        #endregion

		#region // Its_StockOut_Delete
		public DataSet Its_StockOut_Delete(
            string strGwUserCode
            , string strGwPassword
            , string strTid
            , string strPartnerCode
            , string strPartnerUserCode
            , string strLanguageCode
            , string strStockOutID
        )
        {
            #region // temp
            DataSet mdsFinal = CMyDataSet.NewMyDataSet(strTid);
            int nTidSeq = 0;
            bool bNeedTransaction = true;
			string strFunctionName = "Its_StockOut_Delete";
            string strErrorCodeDefault = TError.ErrITS.Its_StockOut_Delete;
            ArrayList alParamsCoupleError = new ArrayList(new object[]{
					"strFunctionName", strFunctionName
                   ,"strCusID",strStockOutID 
					});
            #endregion

            try
            {
                #region // Init:
                _dbCarSv.AppUserID = strPartnerUserCode;
                if (bNeedTransaction) _dbCarSv.BeginTransaction();

                // Write request log:
                _log.WriteLogAsync(
                    _strConfig_GwUserCode // strGwUserCodeForLog
                    , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                    , strTid // strAppTid
                    , strPartnerCode // strAppServiceCode
                    , strPartnerUserCode // strAppUserCode
                    , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                    , TError.Error.NoError // strAppErrorCode
                    , strFunctionName // strAppLogType1
                    , "REQUEST" // strAppLogType2
                    , "" // strAppLogType3
                    , strLanguageCode // strAppLanguageCode
                    , "" // strAppRemark
                    , TUtils.CUtils.ParamsCouple2ParamsTriple(alParamsCoupleError, 1, "").ToArray() // arrobjParamsTriple
                    );

                // Check Valid Tid:
                myUtils_ValidateId(
                    ref alParamsCoupleError // alParamsCoupleError
                    , strTid // strTid
                    , TError.ErrHTC.CommonInvalidTid // strErrCodeWhenInvalid
                    );
                #endregion

                #region //Customer
                #region //Check
                DataTable dt_Book = null;
                this.CheckExistStockOutID
				(
                     ref alParamsCoupleError
					, strStockOutID
                    , out dt_Book
                 );

                #endregion

                #region // Save data
                string strSqlDelete = @"
						delete 
						from Water_StockOut
						where
							StockOutID = @StockOutID
						;
					";

                // Save:
                myDebug_SaveSql(
                    _bDebugMode // bDebugMode
                    , ref alParamsCoupleError // alParamsCoupleError
                    , string.Format("strSql_{0}", strFunctionName) // strParamName
                    , strSqlDelete // strSql
                    );
                _dbCarSv.ExecQuery(
                    strSqlDelete
					, "@StockOutID", strStockOutID
                    );



                #endregion
                #endregion

                // Return Good:
                TDALUtils.DBUtils.CommitSafety(_dbCarSv);
                mdsFinal.AcceptChanges();
                return mdsFinal;
            }
            catch (Exception exc)
            {
                #region // Catch of try:
                // Rollback:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);

                // Return Bad:
                return TUtils.CProcessException.Process(
                    ref mdsFinal
                    , exc
                    , strErrorCodeDefault
                    , alParamsCoupleError.ToArray()
                    );
                #endregion
            }
            finally
            {
                #region // Finally of try:
                // Rollback and Release resources:
                TDALUtils.DBUtils.RollbackSafety(_dbCarSv);
                TDALUtils.DBUtils.ReleaseAllSemaphore(_dbCarSv_Sys, true);

                TUtils.CProcessException.Process(
                    ref mdsFinal // mdsFinal
                    , _log // log
                    , _strConfig_GwUserCode // strGwUserCodeForLog
                    , _strConfig_GwUserPasswordForLog // strGwPasswordForLog
                    , strFunctionName // strFunctionName
                    , TUtils.CUtils.TidNext(strTid, ref nTidSeq) // strAppTid
                    , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") // strAppTDateTime
                    , strPartnerCode // strAppServiceCode
                    , strPartnerUserCode // strAppUserCode
                    , strLanguageCode // strLanguageCode
                    , "" // strRemark
                    );
                #endregion
            }
        }
        #endregion

		void ProcessUpdateCustomerDebitMany(string strCusID, string strDebit, string strNoVoCurrent, string strCuocVoCurrent )
		{
			#region // Save data
			string strSqlDelete = string.Empty;
	
				strSqlDelete = @"
						Update Water_Customer 
						Set Debit=Debit -@Debit
							,NoVoCurrent=NoVoCurrent -@NoVoCurrent	
							,CuocVoCurrent=CuocVoCurrent -@CuocVoCurrent
						where (1=1)
							and CusID = @CusID
							--and ReceiptType='TRA_NO'
						;
					";
			

			// Save:

			_dbCarSv.ExecQuery(
				strSqlDelete
				, "@CusID", strCusID
				, "@Debit", strDebit
				, "@NoVoCurrent", strNoVoCurrent
				, "@CuocVoCurrent", strCuocVoCurrent
				);
			#endregion
		}

        #endregion
        
    }
}
